package com.ruyuan.commodity.common.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruyuan.commodity.model.goods.dos.GoodsDO;
import com.ruyuan.commodity.model.goods.vo.BackendGoodsVO;
import com.ruyuan.commodity.model.goods.vo.BuyCountVO;
import com.ruyuan.commodity.model.goods.vo.GoodsSelectLine;
import com.ruyuan.commodity.model.goods.vo.GoodsSelectorSkuVO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;

/**
 * GoodsMapper的Mapper
 */
@Repository
public interface GoodsMapper extends BaseMapper<GoodsDO> {

    /**
     * 获取新赠商品
     *
     * @param length 获取的查询数量
     * @return
     */
    @ResultType(BackendGoodsVO.class)
    @Select("select * from es_goods where market_enable = 1 and disabled = 1 order by create_time desc limit 0, #{length}")
    List<BackendGoodsVO> newGoods(@Param("length") Integer length);

    /**
     * 更新商品库存
     * @param param 条件
     */
    @Update("update es_goods set enable_quantity=#{param.enable_quantity}, quantity=#{param.quantity} where goods_id=#{param.goods_id}")
    void updateQuantity(@Param("param") Map param);

    /**
     * 根据条件查询多个商品信息
     * @param queryWrapper 查询条件
     * @return
     */
    @ResultType(GoodsSelectLine.class)
    @Select("<script>" +
            " select * from es_goods " +
            " <where>" +
            "   ${ew.sqlSegment}" +
            " </where>" +
            "</script>")
    List<GoodsSelectLine> queryGoodsLines(@Param("ew") QueryWrapper<GoodsDO> queryWrapper);

    /**
     * 查询购买数量
     * @param goodsIds 商品id集合
     * @return
     */
    @ResultType(BuyCountVO.class)
    @Select("<script>" +
            "        select buy_count,goods_id from es_goods " +
            "        <where>" +
            "            <if test='params != null'>" +
            "                goods_id in" +
            "                <foreach collection='params' index='index' item='item' open='(' separator=',' close=')'>" +
            "                    #{item}" +
            "                </foreach>" +
            "            </if>" +
            "        </where>" +
            "</script>")
    List<BuyCountVO> queryBuyCount(@Param("params")List<Long> goodsIds);

    /**
     * 查询sku集合的信息
     * @param skuIds skuid集合
     * @return
     */
    @ResultType(GoodsSelectLine.class)
    @Select("<script>" +
            "select s.*,g.big from es_goods g inner join es_goods_sku s on g.goods_id=s.goods_id " +
            "        <where>" +
            "            <if test='params != null'>" +
            "                s.sku_id in" +
            "                <foreach collection='params' index='index' item='item' open='(' separator=',' close=')'>" +
            "                    #{item}" +
            "                </foreach>" +
            "            </if>" +
            "        </where>" +
            "</script>")
    List<GoodsSelectLine> querySkus(@Param("params")List<Long> skuIds);

    /**
     * 查询sku分页
     * @param iPage 分页参数
     * @param wrapper 条件
     * @return
     */
    @ResultType(GoodsSelectorSkuVO.class)
    @Select("<script>" +
                "select s.* from es_goods_sku s inner join es_goods g on g.goods_id = s.goods_id " +
                "<where>" +
                    "${ew.sqlSegment}" +
                "</where>" +
            "</script>")
    IPage<GoodsSelectorSkuVO> querySkusPage(IPage iPage, @Param("ew")QueryWrapper<GoodsDO> wrapper);
}
